﻿@using LibraryCatalog.Models
@model BookGridModel
@if (Model.CloseDialog) {
    @Const.Close
}
@if (Model.ShowAlreadyDeleted) {
    @Const.AlreadyDeleted
}
<table id="books" class="grid">
    <thead>
        <tr>
            <th>№</th>
            <th>Название</th>
            <th>Автор</th>
            <th>Библиотека</th>
            <th>Действие</th>
        </tr>
    </thead>
    <tbody>
        @for (var i = 0; i < Model.Books.Count; i++) {
            var book = Model.Books[i];
            <tr data-BookId ="@book.BookId">
                <td>@(i + 1)</td>
                <td class="@Const.bookName">@book.Name</td>
                <td class="@Const.bookAuthor">@book.Author</td>
                <td>@book.LibraryName</td>
                <td style="white-space: nowrap">
                    <a href="javascript:void(0)" class="@Const.editBook">Редактировать</a>
                    <a href="javascript:void(0)" class="@Const.deleteBook">Удалить</a>
                </td>
            </tr>
        }
    </tbody>
</table>
<script>
    $(function () {
        var $books = $('#books');
        var grid = $books.dataTable({
            "bPaginate": false,
            "bLengthChange": false,
            "bFilter": false,
            "bInfo": false,
            "sScrollY": "200px",
            "bSort": false,
            "aoColumns": [
                { "sWidth": "1%" },
                null,
                null,
                null,
                { "sWidth": "1%" }
            ]
        });
        $books.data("grid", grid);
        recalSizes();

        $('.@Const.deleteBook').click(function () {
            var parent = $(this).parents('[data-BookId]');
            if (window.confirm("Вы уверены, что хотите удалить книгу '"
                + parent.find('.@Const.bookName').html() + ", "
                + parent.find('.@Const.bookAuthor').html() + "'?"))
                $.ajax({
                    url: '@(Url.Action("DeleteBook"))',
                    data: {
                        bookId: parentsAttr($(this), "data-BookId")
                    },
                    success: function(data) {
                        $('#@Const.BooksGridContainer').html(data);
                    }
                });
        });
        $('.@Const.editBook').click(function () {
            $.ajax({
                url: '@(Url.Action("EditBook"))',
                data: {
                    bookId: parentsAttr($(this), "data-BookId")
                },
                success: function (data) {
                    var regExp = new RegExp('^\\s*@Const.AlreadyDeleted', 'g');
                    if (regExp.exec(data)) {
                        alert('Эта книга кем-то удалена из базы данных.');
                        $('#@Const.BooksGridContainer').html(data.substring(regExp.lastIndex));
                    } else {
                        $('#@Const.editBookDialogContent').html(data);
                        showDialog();
                    }
                }
            });
        });
    })
</script>